<?php
class Shop_Widgets_Mostbuy_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $lang = $request->getParam('lang');
        $count = 4;
        $category_id = $request->getParam('cat');

        //Get connection to database
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();
		
        //Product models access to data
        $productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getProductDao();
        $productDao->setDbConnection($conn);
        $productDao->setLang($lang);

        //Enterprise model access to data
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        //Category models access to data
        $categoryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getCategoryDao();
        $categoryDao->setDbConnection($conn);
        $categoryDao->setLang($lang);

        $products = $productDao->getMostBuy($count, $category_id);
        $this->_view->assign('products', $products);

        $arrPC = array();//Luu gia tri cua category tuong ung voi product
        $arrPE = array();//Luu gia tri enterprise tuong ung voi product
        foreach($products as $product){
            $category_id = substr(substr($product->category_id,1),0,-1);
            $objCategory = $categoryDao->getById($category_id);
            $arrPC[$product->product_id] = $objCategory;

            $objEnterprise = $enterpriseDao->getById($product->enterprise_id);
            $arrPE[$product->product_id] = $objEnterprise;
        }
        $this->_view->assign('arrPC', $arrPC);
        $this->_view->assign('arrPE', $arrPE);
    }
}
